
import { VuexModule, Module, Action, Mutation, getModule } from 'vuex-module-decorators';
import lodash from "lodash";
// import pointType from '../../utils/pointType'
import txzt from '../../utils/txzt'
// console.log(pointType)
import fetch from '../../servers/index'
import store from '@/store';
import { publicAPIModule } from "@/store/modules/public";

// interface
interface point {
  id: string,
  stationId: string,
  [key: string]: any
}
interface socketItem {
  stationId: string,
  id: string,
  [key: string]: any
}
// 判断是否告警
function isGj(item) {
  // let obj={
  //   '0':'注意告警',
  //   '1':'越限告警' ,
  //   '2':'变位告警' ,
  //   '3':'闭合告警' ,
  //   '4':'断开告警' ,
  //   '5':'不告警'
  // }
  let obj = {
    '0': 'gj',
    '1': 'gj',
    '2': 'gj',
    '3': 'gj',
    '4': 'gj',
    '5': 'fgj'
  }
  let isGj = 'fgj'
  if (item.remoteType == '3' || item.remoteType == '4') {
    isGj = 'fgj'
  } else {
    isGj = obj[item.status]
  }
  return isGj
}
// 判断是否异常
function isErr(item) {
  // let obj={
  //   '0':'注意告警',
  //   '1':'越限告警' ,
  //   '2':'变位告警' ,
  //   '3':'闭合告警' ,
  //   '4':'断开告警' ,
  //   '5':'不告警'
  // }
  let obj = {
    '0': 'err',
    '1': 'err',
    '2': 'success',
    '3': 'success',
    '4': 'success',
    '5': 'success'
  }
  let isErr = 'success'
  if (item.remoteType == '3' || item.remoteType == '4') {
    isErr = 'success'
  } else {
    isErr = obj[item.status]
  }

  return isErr
}
// 判断是否在线
function isOnline(value: string) {

  let obj = {
    '0': 'zx',
    '1': 'lx',
  }
  return obj[value]
}
// 判断是否为辅助设备
function isFzsb(xtValue: string) {
  let fzsbArr = ['01', '02', '04', '05', '06']
  if (fzsbArr.indexOf(xtValue) != -1) {
    return true
  } else {
    false
  }

}
export interface GetSocketState {
  zxdw: string[]
  StationArr: string[];
  StationCodeArr: string[];
  socketDataList: point[];
  AllAuxiliaryPointer: point[];
  AllAuxiliaryByStationId: { [key: string]: point[] };
  AllAuxiliaryByPointId: { [key: string]: point };
  AllAuxiliaryByXtId: { [key: string]: point[] };
  AllAuxiliaryBySbId: { [key: string]: point[] };
  AllAuxiliaryByDeviceId: { [key: string]: point[] };
  AllAuxiliaryByCdId: { [key: string]: point[] };
  AllAuxiliaryByBayId: { [key: string]: point[] };
  AllAuxiliaryByAreaId: { [key: string]: point[] };
}
@Module({ dynamic: true, store, name: 'GetSocketStateModule', stateFactory: true, namespaced: true })
class GetSocketStateModule extends VuexModule implements GetSocketState {
  // state
  public zxdw = Object.keys(txzt); //通讯状态点位
  public socketDataList = [] //当前socket数据
  public StationArr = []//所有变电站数组
  public StationCodeArr = []//所有变电站code数组
  public AllAuxiliaryPointer = [] //台账数组
  public AllAuxiliaryByStationId = {}//台账按变电站划分
  public AllAuxiliaryByPointId = {}//台账按点位划分
  public AllAuxiliaryBySbId = {}//台账按设备id划分
  public AllAuxiliaryByXtId = {}//台账按系统id划分
  public AllAuxiliaryByDeviceId = {}//台账按装置id划分
  public AllAuxiliaryByCdId = {}//台账按系统id划分
  public AllAuxiliaryByBayId = {}//台账按间隔划分
  public AllAuxiliaryByAreaId = {}//台账按区域划分

  //  getter 
  // 获取所有台账变电站
  get getterZxdw() {
    let zxdw = lodash.clone(this.zxdw)
    return zxdw
  }
  // 获取当前socket数据
  get getterSocketDataList() {
    let socketDataList = lodash.clone(this.socketDataList)
    return socketDataList
  }
  // 获取所有台账变电站
  get getterAllAuxiliaryByCdId() {
    let AllAuxiliaryByCdId = lodash.clone(this.AllAuxiliaryByCdId)
    return AllAuxiliaryByCdId
  }
  // 按间隔划分
  get getterAllAuxiliaryByBayId() {
    let AllAuxiliaryByBayId = lodash.clone(this.AllAuxiliaryByBayId)
    return AllAuxiliaryByBayId
  }
  // 按区域划分
  get getterAllAuxiliaryByAreaId() {
    let AllAuxiliaryByAreaId = lodash.clone(this.AllAuxiliaryByAreaId)
    return AllAuxiliaryByAreaId
  }
  // 获取所有台账变电站
  get getterStationIdArr() {
    let StationIdArr = lodash.clone(this.StationArr)
    return StationIdArr
  }
  // 获取所有台账变电站code
  get getterStationCodeArr() {
    let StationCodeArr = lodash.clone(this.StationCodeArr)
    return StationCodeArr
  }
  // 获取台账数组
  get getterAllAuxiliaryPointer() {
    let AllAuxiliaryPointer = lodash.clone(this.AllAuxiliaryPointer)
    return AllAuxiliaryPointer
  }
  // 获取台账按变电站划分
  get getterAllAuxiliaryByStationId() {
    let AllAuxiliaryByStationId = lodash.clone(this.AllAuxiliaryByStationId)
    return AllAuxiliaryByStationId
  }
  // 获取台账按装置划分
  get getterAllAuxiliaryByDeviceId() {
    let AllAuxiliaryByDeviceId = lodash.clone(this.AllAuxiliaryByDeviceId)
    return AllAuxiliaryByDeviceId
  }
  // 台账按点位划分 
  get getterAllAuxiliaryByPointId() {
    let AllAuxiliaryByPointId = lodash.clone(this.AllAuxiliaryByPointId)
    return AllAuxiliaryByPointId
  }
  // 台账按设备划分
  get getterAllAuxiliaryBySbId() {
    let AllAuxiliaryBySbId = lodash.clone(this.AllAuxiliaryBySbId)
    return AllAuxiliaryBySbId
  }
  // 台账按系统id划分
  get getterAllAuxiliaryByXtId() {
    let AllAuxiliaryByXtId = lodash.clone(this.AllAuxiliaryByXtId)
    // console.log(AllAuxiliaryByXtId);

    return AllAuxiliaryByXtId
  }
  // 获取变电站状态
  get getterStationState() {
    let StationStateObj = {
      success: [],
      err: [],
    }
    let AllAuxiliaryByStationId = this.AllAuxiliaryByStationId || {}
    for (let key in AllAuxiliaryByStationId) {
      let arr = AllAuxiliaryByStationId[key]
      let status = 'success'
      arr.forEach(item => {
        if (status === 'success') {
          status = item.isErr
        }
      })
      if (status === 'success') {
        StationStateObj.success.push(key)
      } else {
        StationStateObj.err.push(key)
      }
    }
    return StationStateObj
  }
  // 获取变电站风速、温度、湿度、气压
  get getterStationHj() {
    let AllAuxiliaryByStationId = this.AllAuxiliaryByStationId || {}
    let hjobj = {}
    for (let key in AllAuxiliaryByStationId) {
      let obj: any = {
        '040201': { name: '' },//风速监测
        "040202": {}, //风向
        '040102': {},//温度监测
        '040203': {},//温度监测
        '040204': {},//湿度监测
        '040103': {},//湿度监测
        "040206": {}, //  雨量
        '040207': {},//气压监测

      }
      let objKeys = Object.keys(obj)
      let arr = AllAuxiliaryByStationId[key]
      arr.forEach(v => {
        if (objKeys.indexOf(v.dwValue) >= 0) {
          obj[v.dwValue] = {
            value: v.value,
            unit: v.unit
          }
        }
      })
      obj.stationName = arr[0].stationName
      obj.stationId = arr[0].stationId
      hjobj[key] = obj
    }
    return hjobj
  }
  // 获取辅助监控设备数、异常数
  get getterFzjkSbzt() {
    let AllAuxiliaryByXtId = this.AllAuxiliaryByXtId || {}
    let getterFzjkSbzt = {}
    let remoteObj = {}
    // console.log(AllAuxiliaryByXtId);
    for (let key in AllAuxiliaryByXtId) {
      let obj = {
        sbs: 0,
        ycs: 0
      }
      let sbObj = {}
      let itemList = AllAuxiliaryByXtId[key]

      itemList.forEach(v => {
        let sbId = ''
        if (key === '03') {
          sbId = v.equipId
        } else {
          sbId = v.deviceId
        }
        if (sbId) {
          sbObj[sbId] = sbObj[sbId] || 'success'
          if (v.remoteType === '3') {
            remoteObj[sbId] = remoteObj[sbId] || 'success'
            if (remoteObj[sbId] != 'err') {
              remoteObj[sbId] = v.isErr
            }
          }
          if (sbObj[sbId] != 'err') {
            sbObj[sbId] = v.isErr
          }


        }

      })
      for (let key in sbObj) {
        obj.sbs++
        let item = sbObj[key]
        if (item === 'err') {
          obj.ycs++
        }
      }
      getterFzjkSbzt[key] = obj
    }
    let remote = {
      sbs: 0,
      ycs: 0
    }
    for (let key in remoteObj) {
      remote.sbs++
      let item = remoteObj[key]
      if (item === 'err') {
        remote.ycs++
      }
    }
    getterFzjkSbzt['remote'] = remote
    return getterFzjkSbzt
  }
  // Mutation
  // 处理台账信息
  // 设置台账信息
  @Mutation
  setAllAuxiliaryPointer(data: point[]) {
    let AllAuxiliaryByStationId = {} //变电站划分
    let AllAuxiliaryByPointId = {} //点位划分
    let AllAuxiliaryByXtId = {} //系统名称划分
    let AllAuxiliaryBySbId = {} //设备名称划分
    let AllAuxiliaryByCdId = {} //测点名称划分
    let AllAuxiliaryByBayId = {} //间隔划分
    let AllAuxiliaryByAreaId = {} //区域划分
    let AllAuxiliaryByDeviceId = {} //测点装置划分
    let StationObj = {}
    let StationCodeObj = {}
    let log = `以下数据存在问题：`
    let filterFn = point => {
      // if(point.id==='8162#010101#08743'){
      //   console.log(1112,point)
      //   alert(  JSON.stringify(point))
      // }
      if (!point.stationName) {
        log += `${point.id}没有变电站名称，`
      }
      if (!point.sysName) {
        log += `${point.id}没有系统名称，`
      }
      // 没有变电站Id
      if (!point.stationId) {
        log += `${point.id}没有变电站Id，`
        return false
      }
      // 没有系统Id
      if (!point.sysId) {
        log += `${point.id}没有系统Id，`
        return false
      }
      // 没有设备Id
      if (point.xtValue === '03') {
        if (!point.equipName) {
          log += `${point.id}在线监测没有设备名称equipName，`
        }
        if (!point.deviceName) {
          log += `${point.id}在线监测没有装置名称 deviceName`
        }
        if (!point.bayName) {
          log += `${point.id}在线监测没有间隔名称 bayName`
        }
        if (!point.equipId) {
          log += `${point.id}在线监测没有设备Id equipId，`
          return false
        }
        if (!point.deviceId) {
          log += `${point.id}在线监测没有装置Id deviceId`
          return false
        }
        if (!point.bayId) {
          log += `${point.id}在线监测没有间隔Id bayId`
          return false
        }
      } else {
        if (!point.deviceName) {
          log += `${point.id}没有设备名称 deviceName`
        }
        if (!point.deviceId) {
          log += `${point.id}没有设备Id deviceId`
          return false
        }
      }

      // 没有装置类型
      if (!point.deviceType) {
        log += `${point.id}没有装置类型 deviceType`
        return false
      }
      // 智能设备但是没有设备Id
      // if (point.remoteType==='3') {
      //   if(!point.equipId){
      //     return false
      //   }
      // }
      // 没有点位Id
      if (!point.id) {
        log += `${point.id}没有点位Id`
        return false
      }

      return true
    }
    console.log('start')
    data.forEach((v: point) => {
      // console.log(v.id,v.id==='8162#090115#00073')
      try {
        StationObj[v.stationId] = true
        if (v.stationCode) {
          StationCodeObj[v.stationCode] = true
        }
        // publicAPIModule.pointType
        v.xtName = publicAPIModule.pointType.dwTypeObj[v.type].xtName //系统名称
        v.xtValue = publicAPIModule.pointType.dwTypeObj[v.type].xtValue//系统编号
        v.sbValue = publicAPIModule.pointType.dwTypeObj[v.type].sbValue//设备编码
        v.sbName = publicAPIModule.pointType.dwTypeObj[v.type].sbName//设备名称
        v.dwValue = publicAPIModule.pointType.dwTypeObj[v.type].dwValue//单位编码
        v.dwName = publicAPIModule.pointType.dwTypeObj[v.type].dwName//单位名称
        if (filterFn(v)) {
          v.isGj = isGj(v) //是否告警
          v.isErr = isErr(v) //是否异常
          v.isFzsb = isFzsb(v.xtValue) //是否辅助设备
          AllAuxiliaryByPointId[v.id] = v
          AllAuxiliaryByStationId[v.stationId] = AllAuxiliaryByStationId[v.stationId] || []
          AllAuxiliaryByStationId[v.stationId].push(v)
          AllAuxiliaryByXtId[v.xtValue] = AllAuxiliaryByXtId[v.xtValue] || []
          AllAuxiliaryByXtId[v.xtValue].push(v)
          AllAuxiliaryBySbId[v.sbValue] = AllAuxiliaryBySbId[v.sbValue] || []
          AllAuxiliaryBySbId[v.sbValue].push(v)
          AllAuxiliaryByDeviceId[v.deviceId] = AllAuxiliaryByDeviceId[v.deviceId] || []
          AllAuxiliaryByDeviceId[v.deviceId].push(v)
          AllAuxiliaryByCdId[v.dwValue] = AllAuxiliaryByCdId[v.dwValue] || []
          AllAuxiliaryByCdId[v.dwValue].push(v)
          if (v.bayId) {
            AllAuxiliaryByBayId[v.bayId] = AllAuxiliaryByBayId[v.bayId] || []
            AllAuxiliaryByBayId[v.bayId].push(v)
          }
          if (v.area) {
            AllAuxiliaryByAreaId[v.area] = AllAuxiliaryByAreaId[v.area] || []
            AllAuxiliaryByAreaId[v.area].push(v)
          }
          // if(v.stationId==='9bd672cb5f8a0c76107236890201729bd300460260'&&v.isGj==='gj'){
          //   console.log(12121,v)
          // }
          // if(v.id==="8162#020101#09314"){
          //   console.log(123,v)
          // }
        }
      } catch (err) {
        console.log(err);
      }

    })
    console.log('end')
    console.log(log)
    this.StationArr = Object.keys(StationObj)
    this.StationCodeArr = Object.keys(StationCodeObj)
    this.AllAuxiliaryPointer = data;
    this.AllAuxiliaryByStationId = AllAuxiliaryByStationId;
    this.AllAuxiliaryByPointId = AllAuxiliaryByPointId;
    this.AllAuxiliaryByXtId = AllAuxiliaryByXtId;
    this.AllAuxiliaryBySbId = AllAuxiliaryBySbId;
    this.AllAuxiliaryByDeviceId = AllAuxiliaryByDeviceId;
    this.AllAuxiliaryByCdId = AllAuxiliaryByCdId;
    this.AllAuxiliaryByBayId = AllAuxiliaryByBayId;
    this.AllAuxiliaryByAreaId = AllAuxiliaryByAreaId;
    // console.log(11111111, AllAuxiliaryByAreaId);

  }
  // 实时数据添加
  // 处理socket数据
  @Mutation
  setSocketObj(data: { id: string, list: socketItem[] }) {
    data.list.forEach(v => {
      // 修改台账数据

      if (this.AllAuxiliaryByPointId[v.id]) {
        // if (v.id === '130000162#040301#00217') {
        //   console.log(444, v)
        // }
        v.status=v.state
        this.AllAuxiliaryByPointId[v.id].status = v.status
        this.AllAuxiliaryByPointId[v.id].isErr = isErr(v) //是否异常 = v.state
        this.AllAuxiliaryByPointId[v.id].isGj = isGj(v) //是否异常 = v.state
        this.AllAuxiliaryByPointId[v.id].value = v.value
        this.AllAuxiliaryByPointId[v.id].time = v.alertTime
        this.AllAuxiliaryByPointId[v.id].message = v.message
        // v.stationId = data.id
      }
    })
    this.socketDataList = data.list
    this.AllAuxiliaryPointer = lodash.clone(this.AllAuxiliaryPointer)
    this.AllAuxiliaryByStationId = lodash.clone(this.AllAuxiliaryByStationId)
    this.AllAuxiliaryByPointId = lodash.clone(this.AllAuxiliaryByPointId)
    this.AllAuxiliaryByXtId = lodash.clone(this.AllAuxiliaryByXtId)
    this.AllAuxiliaryBySbId = lodash.clone(this.AllAuxiliaryBySbId)
    this.AllAuxiliaryByDeviceId = lodash.clone(this.AllAuxiliaryByDeviceId)
    // console.log(111123, this.AllAuxiliaryByPointId)
    // console.log(111123, this.AllAuxiliaryByPointId['130000162#040301#00217'])

  }
  // Action
  // 获取所有台账信息
  @Action({ commit: 'setAllAuxiliaryPointer' })
  async getAllAuxiliaryPointer() {
    let res = await fetch.getAuxiliaryPointer()
    if (res.data) {
      var Fixed2=res.data
      if (Fixed2 && Fixed2.length > 0) {
        Fixed2.map((v) => {
          if (v.value && v.value.length > 2) {
            v.value = (v.value - 0).toFixed(2) + "";
            var a = v.value.substring(v.value.length - 2);
            // console.log(a,v.value);
            if (a == 0) {
              v.value = v.value.substring(0, v.value.length - 3);
            }
          }
        });
        
      }
    }
    // let data = res.data
    let data = Fixed2
      console.log(data);

    
    if (data && data.length) {
      return data
    } else {
      return []
    }
  }

}

export const GetSocketModule = getModule(GetSocketStateModule) 
